package cn.iocoder.yudao.module.cmt.service.venue;

import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;
import java.util.*;
import cn.iocoder.yudao.module.cmt.controller.admin.venue.vo.*;
import cn.iocoder.yudao.module.cmt.dal.dataobject.venue.VenueInfoDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.cmt.dal.mysql.venue.VenueInfoMapper;
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.cmt.enums.ErrorCodeConstants.*;

@Service
@Validated
public class VenueInfoServiceImpl implements VenueInfoService {

    @Resource
    private VenueInfoMapper venueInfoMapper;

    @Override
    public Long createVenueInfo(VenueInfoSaveReqVO createReqVO) {
        VenueInfoDO venueInfo = BeanUtils.toBean(createReqVO, VenueInfoDO.class);
        venueInfoMapper.insert(venueInfo);
        return venueInfo.getId();
    }

    @Override
    public void updateVenueInfo(VenueInfoSaveReqVO updateReqVO) {
        validateVenueInfoExists(updateReqVO.getId());
        VenueInfoDO updateObj = BeanUtils.toBean(updateReqVO, VenueInfoDO.class);
        venueInfoMapper.updateById(updateObj);
    }

    @Override
    public void deleteVenueInfo(Long id) {
        validateVenueInfoExists(id);
        venueInfoMapper.deleteById(id);
    }

    private void validateVenueInfoExists(Long id) {
        if (venueInfoMapper.selectById(id) == null) {
            throw exception(VENUE_INFO_NOT_EXISTS);
        }
    }

    @Override
    public VenueInfoDO getVenueInfo(Long id) {
        return venueInfoMapper.selectById(id);
    }

    @Override
    public PageResult<VenueInfoDO> getVenueInfoPage(VenueInfoPageReqVO pageReqVO) {
        return venueInfoMapper.selectPage(pageReqVO);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteVenueInfoListByIds(List<Long> ids) {
        ids.forEach(this::validateVenueInfoExists);
        venueInfoMapper.deleteBatchIds(ids);
    }

}
